#!/bin/sh
################################################################################
#    Copyright (c) 2024, 2025 Red Hat Inc and others
#
#    This program and the accompanying materials are made
#    available under the terms of the Eclipse Public License 2.0
#    which is available at https://www.eclipse.org/legal/epl-2.0/
#
#    SPDX-License-Identifier: EPL-2.0
#
#    Contributors:
#        Red Hat Inc - initial API and implementation
#        Eurotech
################################################################################

### SSO Configs

# SSO - Keycloak OpenID Provider
if [ -n "$KEYCLOAK_URL" ] ; then
  echo "Activating OpenID Connect Keycloak integration..."
  echo "  Keycloak: $KEYCLOAK_URL"

  : KEYCLOAK_REALM=${KEYCLOAK_REALM:=kapua}
  : KEYCLOAK_CLIENT_ID=${KEYCLOAK_CLIENT_ID:=rest-api}

  echo "  Keycloak Realm: ${KEYCLOAK_REALM}"
  echo "  OpenId ClientId: ${KEYCLOAK_CLIENT_ID}"

  JAVA_OPTS="$JAVA_OPTS -Dsso.openid.provider=keycloak"
  JAVA_OPTS="$JAVA_OPTS -Dsso.openid.client.id=${KEYCLOAK_CLIENT_ID}"

  test -n "${KAPUA_OPENID_CLIENT_SECRET}" && JAVA_OPTS="$JAVA_OPTS -Dsso.openid.client.secret=${KAPUA_OPENID_CLIENT_SECRET}"

  JAVA_OPTS="$JAVA_OPTS -Dsso.openid.keycloak.uri=${KEYCLOAK_URL}"
  JAVA_OPTS="$JAVA_OPTS -Dsso.openid.keycloak.realm=${KEYCLOAK_REALM}"

# SSO - Generic OpenID Provider
elif [ -n "${KAPUA_OPENID_JWT_ISSUER}" ]; then
  echo "Activating OpenID Connect Generic integration..."
  echo "  OpenID Issuer: ${KAPUA_OPENID_JWT_ISSUER}"

  JAVA_OPTS="${JAVA_OPTS} -Dsso.openid.provider=generic"
  JAVA_OPTS="${JAVA_OPTS} -Dsso.openid.client.id=${KAPUA_OPENID_CLIENT_ID:-rest-api}"

  test -n "${KAPUA_OPENID_CLIENT_SECRET}" && JAVA_OPTS="${JAVA_OPTS} -Dsso.openid.client.secret=${KAPUA_OPENID_CLIENT_SECRET}"

  JAVA_OPTS="${JAVA_OPTS} -Dsso.openid.generic.jwt.audience.allowed=${KAPUA_OPENID_JWT_AUDIENCE:-rest-api}"
  JAVA_OPTS="${JAVA_OPTS} -Dsso.openid.generic.jwt.issuer.allowed=${KAPUA_OPENID_JWT_ISSUER}"

  test -n "${KAPUA_OPENID_AUTH_ENDPOINT}" && JAVA_OPTS="${JAVA_OPTS} -Dsso.openid.generic.server.endpoint.auth=${KAPUA_OPENID_AUTH_ENDPOINT}"
  test -n "${KAPUA_OPENID_LOGOUT_ENDPOINT}" && JAVA_OPTS="${JAVA_OPTS} -Dsso.openid.generic.server.endpoint.logout=${KAPUA_OPENID_LOGOUT_ENDPOINT}"
  test -n "${KAPUA_OPENID_TOKEN_ENDPOINT}" && JAVA_OPTS="${JAVA_OPTS} -Dsso.openid.generic.server.endpoint.token=${KAPUA_OPENID_TOKEN_ENDPOINT}"
  test -n "${KAPUA_OPENID_USERINFO_ENDPOINT}" && JAVA_OPTS="${JAVA_OPTS} -Dsso.openid.generic.server.endpoint.userinfo=${KAPUA_OPENID_USERINFO_ENDPOINT}"
fi

# OpenAPI yaml
openapiFile=webapps/root/doc/openapi.yaml;

if [ -n "$COMMONS_VERSION" ]; then
  # Replace the version and the build number in the openapi file
  awk -v version="$COMMONS_VERSION" -v build_number="$COMMONS_BUILD_NUMBER" '{
      if (!subbed && /version:/) {
          gsub(/version: .*/, "version: " version "-" build_number)
          subbed = 1
      }
      print
  }' "$openapiFile" > "$openapiFile.tmp";

  mv "$openapiFile.tmp" "$openapiFile";
fi;

# Append the Java option for commons.version if defined
if [ -n "$COMMONS_VERSION" ]; then
  echo "Using commons version: $COMMONS_VERSION";
  JAVA_OPTS="${JAVA_OPTS} -Dcommons.version=$COMMONS_VERSION"
fi

# Append the Java option for commons.build.number if defined
if [ -n "$COMMONS_BUILD_NUMBER" ]; then
  echo "Using commons build number: $COMMONS_BUILD_NUMBER";
  JAVA_OPTS="${JAVA_OPTS} -Dcommons.build.number=$COMMONS_BUILD_NUMBER";
fi

# Continue with startup
exec /var/opt/jetty/run-jetty "$@";
